import 'package:fate_blind_box/network/http_request.dart';
import 'package:fate_blind_box/ui/Home/activity/act_list_model.dart';
import 'package:fate_blind_box/ui/cooperation/activity/publish/my_act_item_view.dart';
import 'package:flutter/cupertino.dart';
import '../../../utils/loading.dart';
import '../../../widget/common_empty_view.dart';

class MyActListPage extends StatefulWidget {
  List? dataList = [];
  String tabTitle = '';

  MyActListPage({this.dataList, this.tabTitle = '', Key? key})
      : super(key: key);

  @override
  State<MyActListPage> createState() => _MyActListPageState();
}

class _MyActListPageState extends State<MyActListPage> {
  @override
  Widget build(BuildContext context) {
    return widget.dataList!.isNotEmpty
        ? ListView.builder(
            itemCount: widget.dataList?.length,
            itemBuilder: (BuildContext context, int index) {
              FuturePopular? itemModel = widget.dataList?[index];
              return MyActItemView(
                key: ObjectKey(itemModel),
                dataModel: itemModel,
                tabTitle: widget.tabTitle,
                delAction: (res) {
                  setState(() {
                    deleteAct(itemModel ?? FuturePopular());
                  });
                },
              );
            })
        : const CommonEmptyView();
  }

  void deleteAct(FuturePopular itemModel) async {
    var res = await HttpRequest.request('member/activity/delActi',
        params: {"activityId": itemModel.activityId});
    if (res['code'] == 0) {
      final data = res['data'];
      if (data != null) {
        if (widget.dataList!.isNotEmpty) {
          widget.dataList!.remove(res);
        }
        toast('保存成功');
        setState(() {});
      } else {
        toast(res['msg']);
      }
    }
  }

  @override
  void initState() {
    super.initState();
    widget.dataList ??= [];
  }
}
